import 'reflect-metadata';
import { Router } from 'express';
export const router = Router();

enum Method {
  get = 'get',
  post = 'post'
}

export function controller(target: any) {
  for (let key in target.prototype) {
    const path = Reflect.getMetadata('path', target.prototype, key);
    const handler = target.prototype[key];
    const method: Method = Reflect.getMetadata('method', target.prototype, key);
    if (path && handler && method) {
      router[method](path, handler);
    }
  }
}

export function get(path: string) {
  return function(target: any, key: string) {
    Reflect.defineMetadata('path', path, target, key);
    Reflect.defineMetadata('method', 'get', target, key);
  };
}
export function post(path: string) {
  return function(target: any, key: string) {
    Reflect.defineMetadata('path', path, target, key);
    Reflect.defineMetadata('method', 'post', target, key);
  };
}
